package com.dearbinge.opentapi.content.user;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.user.UserBlackListService;
import com.dearbinge.data.pojo.user.vo.UserBlacklistRequestVO;
import com.dearbinge.data.pojo.user.vo.UserBlacklistResponseVO;
import com.dearbinge.utils.BeanValidator;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.HashMap;
import java.util.Map;

/**
 * 黑名单控制层
 *
 * @author wes
 * @date 2019/4/8
 */
@Api(tags = "3.2、用户-黑名单")
@RestController
@CrossOrigin
@RequestMapping("/blackList")
@EnableAutoConfiguration
public class UserBalckListController extends WebMvcConfigurerAdapter {

    @Autowired
    private UserBlackListService userBlackListService;

    @ApiOperation(value = "新增",notes = "新增")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultMsg<String> add(@RequestBody UserBlacklistRequestVO userBlacklistRequestVO){
        try {
            //校验参数非空
            Map<String, Object> paramMap = new HashMap<String, Object>(16);
            paramMap.put("myId", userBlacklistRequestVO.getMyId());
            paramMap.put("blackUserId", userBlacklistRequestVO.getBlackUserId());
            Map<String, Object> checkParam = BeanValidator.checkParam(paramMap);
            if (!(boolean) checkParam.get(BeanValidator.FLAG)) {
                return ResultMsg.error(ResultStatusCodeEnum.CODE5001.getCode(), checkParam.get("name") + "参数为空");
            }


            int insert = userBlackListService.insert(userBlacklistRequestVO);
            if (insert > 0){
                return ResultMsg.ok("新增成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    @ApiOperation(value = "删除",notes = "删除")
    @ApiImplicitParam(name = "blackId",value = "表id",paramType = "query",required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    private ResultMsg<String> delete(Integer blackId){
        try {
            //校验参数非空
            Map<String, Object> paramMap = new HashMap<String, Object>(16);
            paramMap.put("blackId", blackId);
            Map<String, Object> checkParam = BeanValidator.checkParam(paramMap);
            if (!(boolean) checkParam.get(BeanValidator.FLAG)) {
                return ResultMsg.error(ResultStatusCodeEnum.CODE5001.getCode(), checkParam.get("name") + "参数为空");
            }
            if (userBlackListService.delete(blackId) > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    @ApiOperation(value = "查询黑名单",notes = "查询黑名单")
    @ApiImplicitParam(name = "myId",value = "我的用户id",paramType = "query",required = true)
    @ApiResponses({
                @ApiResponse(response = UserBlacklistResponseVO.class, code = 200, message = "返回结果参数")
    })
    @RequestMapping(value = "/listByMyId",method = RequestMethod.GET)
    private ResultMsg<Page> listByMyId(Integer myId, Integer current, Integer size){
        try {
            //校验参数非空
            Map<String, Object> paramMap = new HashMap<String, Object>(16);
            paramMap.put("myId", myId);
            Map<String, Object> checkParam = BeanValidator.checkParam(paramMap);
            if (!(boolean) checkParam.get(BeanValidator.FLAG)) {
                return ResultMsg.error(ResultStatusCodeEnum.CODE5001.getCode(), checkParam.get("name") + "参数为空");
            }
            Page list = userBlackListService.listByMyId(myId, current, size);
            return ResultMsg.ok(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    @ApiOperation(value = "是否加黑",notes = "是否拉黑 未拉黑：返回0； 已拉黑：返回blackId的值。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "myId",value = "我的用户id",paramType = "query",required = true),
            @ApiImplicitParam(name = "blackUserId",value = "拉黑的用户id",paramType = "query",required = true)
    })
    @RequestMapping(value = "/isBlack",method = RequestMethod.GET)
    private ResultMsg<Integer> isBlack(Integer myId, Integer blackUserId){
        try {
            //校验参数非空
            Map<String, Object> paramMap = new HashMap<String, Object>(16);
            paramMap.put("myId", myId);
            paramMap.put("blackUserId", blackUserId);
            Map<String, Object> checkParam = BeanValidator.checkParam(paramMap);
            if (!(boolean) checkParam.get(BeanValidator.FLAG)) {
                return ResultMsg.error(ResultStatusCodeEnum.CODE5001.getCode(), checkParam.get("name") + "参数为空");
            }
            return  userBlackListService.isBlack(myId, blackUserId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
